import {getVoiceToken} from "@/utils/api/api.js";

// 2022-07-28 小谢 车牌识别
const audioTeam = [];
let audioStartSwitch = false;
const getAudioUrl = 'https://tsn.baidu.com/text2audio';


// access_token	通过API Key和Secret Key获取的access_token,参考“Access Token获取”
// https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
// https://ai.baidu.com/ai-doc/OCR/ck3h7y191
// https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=24.397688a0243da06f608c389de025214f.2592000.1661586929.282335-26825378&url=xxx

// {"words_result":{"number":"赣AVK685","vertexes_location":[{"x":433,"y":1413},{"x":699,"y":1409},{"x":699,"y":1485},{"x":434,"y":1488}],"color":"blue","probability":[0.9997380376,0.9999591112,0.9999936819,0.9999703169,0.9999967813,0.9999793768,0.9999849796,0.9999460578]},"log_id":1552563965629228006}
function getBDVoicToken() {
	return new Promise((rs, rj) => {
		// 陶
		let APIKey = "bXM3X2wVTDkrWavxYhETp5Yw";
		let SecretKey = "LwnQGgFxxga2q2UI9YM1Gnxww1NbVzvm";
		
		let urla = `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${APIKey}&client_secret=${SecretKey}`
		// console.info("urla",urla);
		uni.request({ // 强烈建议此接口由后端访问并且维护token有效期，否则前端每次访问都会刷新token
			//此url为专门插件测试预览用的key和secret key， 请替换为自己申请的key
			
			url: urla,
			method: 'GET', //建议使用post访问
			data: ``,
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			success: (res) => {
				if (res.data && res.data.access_token) {
					// console.log('token: ' + res.data.access_token);
					rs(res.data.access_token);
				} else {
					//console.log('获取语音tok接口为空');
					rj('获取语音tok接口为空');
				}
				
				rs(res);
			},
			fail: (err) => {
				rj(err);
			}
		})
		
		
	})
}



export  function DiscernLicenseplate(paramsobj) { 
	return new Promise((reslove,reject)=>{
		//获取token
		getBDVoicToken({
			
		}).then((token)=>{
			//识别车牌号
			// https://ai.baidu.com/ai-doc/OCR/ck3h7y191
			let urla = `https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=${token}&url=${paramsobj.cart_img}&multi_scale=true`
			console.info("urla",urla);
			uni.request({
				url: urla,
				method: 'GET', //建议使用post访问
				data: ``,
				header: {
					"content-type": "application/x-www-form-urlencoded"
				},
				success: (res) => {
					reslove(res);//返回识别的车牌号
				},
				fail: (err) => {
					reject(err);
				}
			})
			
		}).catch((err)=>{
			reject(err);
		})
	})
}
